package com.dhu.graph.utils;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-03-26
 **/
public class Bag<Item> implements Iterable<Item>{
    private Node first;
    private int N;

    public Bag(){
    }
    public void put(Item value){
        Node node = new Node(value);
        node.next = first;
        first = node;
        N++;
    }

    @Override
    public void forEach(Consumer<? super Item> action) {

    }

    @Override
    public Iterator<Item> iterator() {
        return new Iterator<Item>() {
            Node p = first;
            @Override
            public boolean hasNext() {
                return p !=null;
            }

            @Override
            public Item next() {
                Node ret = p;
                p = p.next;
                return ret.value;
            }
        };
    }


    private class Node{
        Item value;
        Node next;

        Node(Item value){
            this.value = value;
        }
    }

}
